//
//  LocationViewController.m
//  播放音乐(AVPlayer)
//
//  Created by 刘家俊 on 17/4/7.
//  Copyright © 2017年 Andrew. All rights reserved.
//

#import "LocationViewController.h"
#import <AVFoundation/AVFoundation.h>

@interface LocationViewController ()

/* 播放器 */
@property (nonatomic, strong) AVPlayer *player;

@end

@implementation LocationViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
}

#pragma mark - 懒加载
- (AVPlayer *)player
{
    if (!_player) {
        //1.创建单乐资源
        NSURL *url = [[NSBundle mainBundle] URLForResource:@"14945107.mp3" withExtension:nil];
        
        AVPlayerItem *playerItem = [AVPlayerItem playerItemWithURL:url];
        
        //创建播放器
        //        _player = [AVPlayer playerWithURL:url]; //这个不能改变音乐的动态
        _player = [AVPlayer playerWithPlayerItem:playerItem];//可以改变音乐的动态
        
    }
    return _player;
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [self.player play];
    
//    //切换另外一首音乐
//    AVPlayerItem *nextPlayItem = [AVPlayerItem playerItemWithURL:nil];
//    [self.player replaceCurrentItemWithPlayerItem:nextPlayItem];
}

@end
